<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Interest gateway class for communicating with persistent storage
 *
 * @category    RedCross
 * @package     Core_Gateway
 */
class Core_Gateway_Interest extends RedCross_Gateway_GatewayAbstract
{
    /**
     * Constructor
     * Defines some private variables
     */
    public function __construct()
    {
        $this->_class = 'Interest';
        $this->_modelPath = 'Core_Model';
    }
    
    public function fetchAllInterestsForMember($memberId)
    {
        $date = new Zend_Date();
        $dateNow = $date->get('YYYY-MM-dd HH:mm:ss');
        $result = Doctrine_Query::create()
            ->select('i.event_id, e.name, e.from_date, e.to_date, m.id')
            ->from('Core_Model_Interest i')
            ->leftJoin('i.Event e ON i.event_id = e.id')
            ->leftJoin('i.Member m ON i.member_id = m.id')
            ->addWhere('e.from_date > ?', $dateNow)
            ->addWhere('m.id = ?', $memberId)
            ->fetchArray();
        
        return $result;
    }
    
    public function checkIfMemberHasInterestForEvent($memberId, $eventId)
    {
        $query = Doctrine_Query::create()
            ->select('i.event_id, i.member_id')
            ->from('Core_Model_Interest i')
            ->addWhere('i.event_id = ?', $eventId)
            ->addWhere('i.member_id = ?', $memberId)
            ->fetchArray();
        
        return count($query) > 0;
        
    }
    
    public function deleteInterestFromEventForMember($memberId, $eventId)
    {
        $query = Doctrine_Query::create()
            ->delete('Core_Model_Interest i')
            ->addWhere('i.event_id = ?', $eventId)
            ->addWhere('i.member_id = ?', $memberId);
            
        return $query->execute();
            
    }
}